package com.beans;

import com.logic.HostAdmin;
import com.entity.Host;
import com.utils.SortableList;
import com.utils.XMLParser;
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import javax.faces.bean.ManagedBean;

/**
 * <p> A basic backing bean for a ice:dataTable component. This bean contains a
 * Collection of IventoryItem objects which is used as the dataset for a
 * dataTable component. Each instance variable in the InventoryItem obejct is
 * represented as a column in the dataTable component. </p> <p>This Bean also
 * extends SortableList to aid in making the dataTable sortable via
 * commandSortHeader component. </p>
 *
 */
@ManagedBean
public class HostList extends SortableList {

    //  Lista de hosts.
    private static Host[] hostList = loadList();
    // Nombres de columna
    private static final String hostName = "Nombre de Host";
    private static final String hostCPU = "Procesador";
    private static final String cpuSpeed = "Vel. de Procesador (MHz)";
    private static final String hyperthreading = "Hyperthreading";
    private static final String coreNumber = "Numero de Núcleos";
    private static final String useOfCPU = "Uso CPU (%)";
    private static final String memory = "Memoria (MB)";
    private static final String swap = "Swap (MB)";
    private static final String memoryUse = "Uso Memoria (%)";
    private static final String operativeSystem = "Sistema Operativo";
    private static final String kernel = "Version Kernel";
    private static final String hostType = "Tipo Host";
    
    //Atributos de visibilidad de los detalles de host
    private static boolean renderedDetails = false;
    private static boolean renderedTable = true;
    
    //Host elegido para visualizacion
    private Host selectedHost;
    
    public HostList() {
        super(hostName);
    }

    public void showDetails(Host host) {
        setSelectedHost(host);
        switchRenderedDetails();
    }

    public void switchRenderedDetails() {
        renderedDetails = renderedDetails ? false : true;
        renderedTable = renderedTable ? false : true;
    }

    public boolean getRenderedDetails() {
        return renderedDetails;
    }

    public boolean getRenderedTable() {
        return renderedTable;
    }

    public String getHostName() {
        return hostName;
    }

    public String getHostCPU() {
        return hostCPU;
    }

    public String getCPUVel() {
        return cpuSpeed;
    }

    public String getHyperthreading() {
        return hyperthreading;
    }

    public String getCoreNumber() {
        return coreNumber;
    }

    public String getUseCPU() {
        return useOfCPU;
    }

    public String getHostType() {
        return hostType;
    }

    public String getKernel() {
        return kernel;
    }

    public String getHostMemory() {
        return memory;
    }

    public String getMemoryUse() {
        return memoryUse;
    }

    public String getOperativeSystem() {
        return operativeSystem;
    }

    public String getHostSwap() {
        return swap;
    }

    /**
     * Gets the inventoryItem array of car data.
     *
     * @return array of car inventory data.
     */
    public Host[] getHostsList() {
        loadList();
        // we only want to sortColumnName if the column or ordering has changed.
        if (!oldSort.equals(sortColumnName) || oldAscending != ascending) {
            sort();
            oldSort = sortColumnName;
            oldAscending = ascending;
        }
        return hostList;
    }

    public void setSelectedHost(Host selectedHost){
        this.selectedHost=selectedHost;
        HostAdmin.getInstance().setHost(selectedHost);
    }
    
    public Host getSelectedHost() {
        return selectedHost;
    }

    /**
     * Determines the sortColumnName order.
     *
     * @param sortColumn to sortColumnName by.
     * @return whether sortColumnName order is ascending or descending.
     */
    protected boolean isDefaultAscending(String sortColumn) {
        return true;
    }

    /**
     * Sorts the list of car data.
     */
    protected void sort() {
        Comparator comparator = new Comparator() {
            public int compare(Object o1, Object o2) {
                Host h1 = (Host) o1;
                Host h2 = (Host) o2;
                if (sortColumnName == null) {
                    return 0;
                }
                if (sortColumnName.equals(hostName)) {
                    return ascending
                            ? h1.getHostName().compareTo(h2.getHostName())
                            : h2.getHostName().compareTo(h1.getHostName());
                } else if (sortColumnName.equals(hostCPU)) {
                    return ascending ? h1.getHostCPU().compareTo(h2.getHostCPU())
                            : h2.getHostCPU().compareTo(h1.getHostCPU());
                } /*
                 * else if (sortColumnName.equals(cpuSpeed)) { return ascending
                 * ? h1.getCpuVel()>h2.getCpuVel() :
                 * h2.getCpuVel()>h1.getCpuVel(); }
                 */ else if (sortColumnName.equals(hyperthreading)) {
                    return ascending
                            ? new Integer(h1.getHyperthreading()).compareTo(new Integer(h2.getHyperthreading()))
                            : new Integer(h2.getHyperthreading()).compareTo(new Integer(h2.getHyperthreading()));
                } else if (sortColumnName.equals(coreNumber)) {
                    return ascending
                            ? new Integer(h1.getCoreNumber()).compareTo(new Integer(h2.getCoreNumber()))
                            : new Integer(h2.getCoreNumber()).compareTo(new Integer(h1.getCoreNumber()));
                } else if (sortColumnName.equals(memory)) {
                    return ascending
                            ? new Integer(h1.getMemory()).compareTo(new Integer(h2.getCoreNumber()))
                            : new Integer(h2.getMemory()).compareTo(new Integer(h1.getCoreNumber()));
                } else if (sortColumnName.equals(memoryUse)) {
                    return ascending
                            ? new Integer(h1.getCoreNumber()).compareTo(new Integer(h2.getCoreNumber()))
                            : new Integer(h2.getCoreNumber()).compareTo(new Integer(h1.getCoreNumber()));
                } else if (sortColumnName.equals(swap)) {
                    return ascending
                            ? new Integer(h1.getSwap()).compareTo(new Integer(h2.getSwap()))
                            : new Integer(h2.getSwap()).compareTo(new Integer(h1.getSwap()));
                } else if (sortColumnName.equals(operativeSystem)) {
                    return ascending
                            ? new Integer(h1.getOperativeSistem()).compareTo(new Integer(h2.getOperativeSistem()))
                            : new Integer(h2.getOperativeSistem()).compareTo(new Integer(h1.getOperativeSistem()));
                } else if (sortColumnName.equals(kernel)) {
                    return ascending
                            ? new Integer(h1.getKernel()).compareTo(new Integer(h2.getKernel()))
                            : new Integer(h2.getKernel()).compareTo(new Integer(h1.getKernel()));
                } else if (sortColumnName.equals(hostType)) {
                    return ascending
                            ? new Integer(h1.getHostType()).compareTo(new Integer(h2.getHostType()))
                            : new Integer(h2.getHostType()).compareTo(new Integer(h1.getHostType()));
                } else {
                    return 0;
                }
            }
        };
        Arrays.sort(hostList, comparator);
    }

    public static Host[] loadList() {

        File dataDir = new File("/ring/files/");

        String hostDirs[] = dataDir.list();
        Host[] list = new Host[hostDirs.length];

        for (int i = 0; i < hostDirs.length; i++) {
            String path = "/ring/files/" + hostDirs[i] + "/";
            list[i] = XMLParser.getInstance().parseHostXMLFiles(path);
        }
        return list;
    }

    void setHostList(Host[] loadList) {
        hostList=loadList;
    }
}
